package ren.nearby.http.request;


import com.caimuhao.rxpicker.utils.T;
import com.orhanobut.logger.Logger;
import com.tougc.bean.beans.Successful14K;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;
import ren.nearby.http.exception.ApiException;


public class RxResultHelper {
    //http://www.jianshu.com/p/b1da0387f805  MVP+Rxjava+Retrofit设计
    //https://juejin.im/post/5a94c19d6fb9a063577523a8 Retrofit统一异常处理


    /**
     * 无
     *
     * @param <T>
     * @return
     */
    public static <T> ObservableTransformer<HttpResultBean14K<T>, T> handleResult14K() {
        return new ObservableTransformer<HttpResultBean14K<T>, T>() {
            @Override
            public ObservableSource<T> apply(@NonNull Observable<HttpResultBean14K<T>> upstream) {
                return upstream.flatMap(
                        new Function<HttpResultBean14K<T>, ObservableSource<T>>() {
                            @Override
                            public Observable<T> apply(HttpResultBean14K<T> result) {
                                if (result.code == HttpResultBean14K.SUCCESS) {
                                    Logger.e("code 进来了 next...");
                                    return createData2(result.data, null, null);
                                } else if (result.code == HttpResultBean14K.FAILURE) {
                                    Logger.e("code 进来了error...");
                                    return Observable.error(new ApiException(result.code + "-" + result.msg));
                                }
                                return Observable.empty();
                            }
                        }
                );
            }
        };
    }

    /**
     * @param t
     * @param msg
     * @param instanceSucces null 则不提取成功信息 not null 则提取成功信息
     * @param <T>
     * @return
     */
    public static <T> Observable<T> createData2(final T t, final String msg, final InstanceSucces instanceSucces) {
        return Observable.create(
                new ObservableOnSubscribe<T>() {
                    @Override
                    public void subscribe(@NonNull ObservableEmitter<T> emitter) throws Exception {
                        try {
                            if (instanceSucces != null) {
                                instanceSucces.succes(msg);
                            }
                            T tt;
                            if (t == null) {
                                Successful14K k14 = new Successful14K();
                                tt = (T) k14;
                                emitter.onNext(tt);
                                emitter.onComplete();
                            }else{
                                emitter.onNext(t);
                                emitter.onComplete();
                            }
                        } catch (Exception e) {
                            Logger.e("进来了" + e.getMessage() + " - " + e.toString());
                            emitter.onError(e);
                        }
                    }
                });
    }
}

